package logic

import (
	"context"
	"errors"
	"strings"
	"video-guard/models/mysql"
	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *videorpc.CommentAddTRequest) (*videorpc.CommentAddTResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("未登录")
	}
	var video mysql.VideoInfo
	err = video.FindVidepByVideoId(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("未有视频，不能评论")
	}
	if in.CommentContent == "" {
		return nil, errors.New("请输入评论内容")
	}
	if strings.Contains("垃圾", in.CommentContent) || strings.Contains("骗人", in.CommentContent) {
		return nil, errors.New("含违规词汇，请修改")
	}

	comment := &mysql.CommentInfo{
		CommentContent: in.CommentContent,
		CommentStatus:  in.CommentStatus,
		IsAnonymous:    in.IsAnonymous,
	}
	err = comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &videorpc.CommentAddTResponse{
		CommentId: int64(comment.ID),
	}, nil
}
